Debunking C++ Myths by Alexandru Bolboacă | Ferenc-Lajos Deák

Debunking C++ Myths by Alexandru Bolboacă | Ferenc-Lajos Deák

Author:Alexandru Bolboacă | Ferenc-Lajos Deák
Language: eng
Format: epub
Publisher: Packt Publishing Pvt Ltd
Published: 2024-11-25T00:00:00+00:00


The dark orders of C++

There is one dark corner of the C++ language that is rarely touched by sunlight, and if a piece of code from these depths surfaces by any chance, a gang of hardcore developers immediately jumps on it and refactors it into digestible bits and bytes. Let’s consider, for example, the very simple case of why the a[2] and 2[a] expressions are equivalent when in C++, and a is an array of objects:

int main() { int a[16] = {0}; a[2] = 3; 3[a] = 4; }

The preceding piece of code, despite the fact that it looks ugly, actually compiles. The reason is the following: in C++, the operator [] array subscript is defined in terms of pointer arithmetic. The a[i] expression is translated by the compiler into *(a + i) , where a is a pointer to the first element of the array and i is the index. The i[a] expression at the end is also translated to the *(i + a) expression, where i is the index and a is the pointer to our arrays’ first elements.

Since addition is commutative for the compiler, it does not really matter which comes first.

So, we have found a specific case where order doesn’t really matter in C++. But this works only for old-style C arrays; std::vector and std::array do not accept this kind of out-of-order syntax. There is a very specific reasoning for that; the subscript operator for std::vector and std::array does not support the commutative behavior seen in raw arrays, namely the following:

Operator overloading : The operator[] for std::vector and std::array is a member function, meaning it needs to be called on an instance of the class. It cannot be invoked with the index first, as member functions require the object to be on the left side of the call.

No pointer arithmetic : The internal implementation of std::vector and std::array does not rely on raw pointer arithmetic for indexing. They manage their memory and bounds checking differently, ensuring safer access to elements.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(56079)
What's Done in Darkness by Kayla Perrin(26617)
The Fifty Shades Trilogy & Grey by E L James(19095)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19079)
Shot Through the Heart by Mercy Celeste(18952)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17132)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17019)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16895)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16840)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16467)
The Subtle Art of Not Giving a F*ck by Mark Manson(14382)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14158)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13669)
Scorched Earth by Nick Kyme(12785)
Drei Generationen auf dem Jakobsweg by Stein Pia(10982)
Suna by Ziefle Pia(10902)
Scythe by Neal Shusterman(10366)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(10251)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(10225)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(10216)